/**
 * Created by macos on 14/11/11.
 */

module.exports = {

  types:{
    description:function(value) {
      return true;
    }
  },

  attributes : {
    uuid: {
      type: 'string',
      required: true,
      unique: true,
      description :'设备UUID'
    },
    userId: {
      type: 'string',
      required: true,
      description :'用户编号'
    },
    username: {
      type: 'string',
      required: true,
      description :'用户名'
    },
    model: {
      type: 'string',
      required: false,
      description :'设备型号'
    },
    os: {
      type: 'string',
      required: false,
      description :'操作系统'
    }
  },

  beforeValidate: function(values, next) {
    User.findOne({username: values.username})
      .exec(function (error, result) {
        if (error) {
          return next(error)
        }

        if (_.has(result, 'id')) {
          values.userId = result.id;
          next();
        } else {
          return next('username is not exist!');
        }
      });
  }

}